/**
 * Created by hda on 2017/5/22.
 */
define([], function () {
    var model = {};

    model.regex = {
        // required: /^\s*$/,
        require: /^[^\s]+$/,
        required: /^[^\s]+$/,
        number: /^[0-9]+$/,
        int: /^\-?[0-9]+$/,
        decimal: /^\-?[0-9]*\.?[0-9]+$/,
        email: /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
        mobile: /^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\d{8}$/,
        zipcode: /[1-9]{1}{\d+}{5}/
    }

    model.run = function (arrObj, rule) {
        var obj = model.arrayTojson(arrObj)
        console.log(obj)
        //表单对象，验证规则

        // var obj = {
        //     title: '',
        //     price: 1
        // }
        // var rule = {
        //     price: {
        //         require_error: '必须填写',
        //         verify_error: '验证失败',
        //         rules: 'required|regex',// 身份证   电话  邮箱
        //         regex: /^[0-9]+$/
        //
        //     },
        //     title: {
        //         require_error: '',
        //         verify_error: '验证失败',
        //         rules: 'required',// 身份证   电话  邮箱
        //         // range:',6'
        //     }
        // }

        //循环rule
        for (var m in rule) {
            if (typeof (obj[m]) != 'undefined') {
                var json = model.do(obj[m], rule[m]['rules'], rule[m]['regex']);
                if (!json.flag) {
                    var error_text = '';
                    // console.log('不通过不通过')
                    if (json.type == 'required') {
                        if (typeof(rule[m]['require_error']) != 'undefined' && rule[m]['require_error'] != '') {
                            if (typeof(rule[m]['text']) != 'undefined' && rule[m]['text'] != '') {
                                error_text = rule[m]['text'] + rule[m]['require_error'];
                            } else {
                                error_text = rule[m]['require_error'];
                            }
                        } else {
                            if (typeof(rule[m]['text']) != 'undefined' && rule[m]['text'] != '') {
                                error_text = rule[m]['text'] + '必须填写';
                            } else {
                                error_text = m + '必须填写';
                            }
                        }
                    } else {
                        if (typeof(rule[m]['text']) != 'undefined' && rule[m]['text'] != '') {
                            error_text = rule[m]['text'] + rule[m]['verify_error'];
                        } else {
                            error_text = rule[m]['verify_error'];
                        }
                    }
                    mui.alert(error_text, '表单验证提示')
                    return false;
                } else {
                    // console.log('测试通过')
                }
            }
        }

        return true;

    }


    model.do = function (val, rules, regex) {
        val = val.replace(/(^\s*)|(\s*$)/g, "");//删除左右两端的空格
        var arr = rules.split('|');
        for (var i = 0; i < arr.length; i++) {
            var regex_now;
            var rule = arr[i];
            if (typeof(model.regex[rule]) != 'undefined') {
                regex_now = model.regex[rule];
            }
            if (rule == 'regex') {
                regex_now = regex;
            }
            var re = new RegExp(regex_now);
            if (!re.test(val)) {
                var json = {
                    flag: false,
                    type: rule
                };
                return json;
            }
        }
        return {flag: true};
    }


    model.arrayTojson = function (arrObj) {
        var serializeObj = {};
        var array = arrObj;
        $(array).each(function () {
            if (serializeObj[this.name]) {
                if ($.isArray(serializeObj[this.name])) {
                    serializeObj[this.name].push(this.value);
                } else {
                    serializeObj[this.name] = [serializeObj[this.name], this.value];
                }
            } else {
                serializeObj[this.name] = this.value;
            }
        });
        return serializeObj;
    }

    return model;
});